#include "dialogs/DialogOptions.h"

#include "scenes/SceneMainMenu.h"

#include "dialogs/DialogControls.h"

#define OPTION_NAME_SIZE 17

DialogOptions::DialogOptions( GameApplication& theApp, gdn::Scene& theScene )
   : App( theApp ),
     Scene( theScene )
{
	// None
}

void DialogOptions::Initialize()
{
	App.GameConfig.LoadFromFile();

	background.SetImage( *App.Images.GetResource( App.GameResources[ "gui dialog background" ] ) );

	gdn::Image* sliderScaleImage  = App.Images.GetResource( App.GameResources[ "gui slider scale" ] );
	gdn::Image* sliderSliderImage = App.Images.GetResource( App.GameResources[ "gui slider slider" ] );
	gdn::Image* checkboxImage     = App.Images.GetResource( App.GameResources[ "gui checkbox particles" ] );
	gdn::Image* buttonLargeImage  = App.Images.GetResource( App.GameResources[ "gui button large" ] );
	gdn::Image* buttonSmallImage  = App.Images.GetResource( App.GameResources[ "gui button small" ] );
	gdn::Font * font              = App.Fonts.GetResource( App.GameResources[ "font sansation" ] );

	soundText.SetString( "Sound: " );
	soundText.SetFont( *font );
	soundText.SetColor( gdn::Color( 0, 0, 0 ) );
	soundText.SetCharacterSize( OPTION_NAME_SIZE );
	soundText.SetPosition( gdn::Vector2f( 10, 55 ) );
	soundSlider = gui::Slider( gdn::Vector2f( 75, 50 ), 268 - 65 - 10 - 10,
			                   0, 200, App.GameConfig.GetSoundVolume(),
			                   *sliderScaleImage, *sliderSliderImage,
			                   gdn::Rectanglei( 0, 0, 2, 32 ), gdn::Rectanglei( 2, 0, 1, 32 ), gdn::Rectanglei( 3, 0, 2, 32 ),
							   gdn::Rectanglei( 0, 0, 10, 32 ), gdn::Rectanglei( 10, 0, 10, 32 ), gdn::Rectanglei( 20, 0, 10, 32 ) );

	musicText.SetString( "Music: " );
	musicText.SetFont( *font );
	musicText.SetColor( gdn::Color( 0, 0, 0 ) );
	musicText.SetCharacterSize( OPTION_NAME_SIZE );
	musicText.SetPosition( gdn::Vector2f( 10, 86 ) );
	musicSlider = gui::Slider( gdn::Vector2f( 75, 84 ), 268 - 65 - 10 - 10,
			                   0, 200, App.GameConfig.GetMusicVolume(),
			                   *sliderScaleImage, *sliderSliderImage,
			                   gdn::Rectanglei( 0, 0, 2, 32 ), gdn::Rectanglei( 2, 0, 1, 32 ), gdn::Rectanglei( 3, 0, 2, 32 ),
							   gdn::Rectanglei( 0, 0, 10, 32 ), gdn::Rectanglei( 10, 0, 10, 32 ), gdn::Rectanglei( 20, 0, 10, 32 ) );

	particlesText.SetString( "Particles: " );
	particlesText.SetFont( *font );
	particlesText.SetColor( gdn::Color( 0, 0, 0 ) );
	particlesText.SetCharacterSize( OPTION_NAME_SIZE );
	particlesText.SetPosition( gdn::Vector2f( 10, 115 ) );
	particlesCheckbox = gui::Checkbox( gdn::Vector2f( 90, 117 ), App.GameConfig.GetParticlesEnabled(),
					                   *checkboxImage,
					                   gdn::Rectanglei( 0, 0, 20, 20 ), gdn::Rectanglei( 20, 0, 20, 20 ), gdn::Rectanglei( 40, 0, 20, 20 ),
					                   gdn::Rectanglei( 60, 0, 20, 20 ), gdn::Rectanglei( 80, 0, 20, 20 ), gdn::Rectanglei( 100, 0, 20, 20 ) );

	controlsButton = gui::Button( gdn::Vector2f( 46, 200 - 48 - 10 ),
							      "Controls", "Controls", "Controls",
							      gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				                  *buttonLargeImage, *font, 19,
				                  gdn::Rectanglei( 0, 0, 175, 48 ), gdn::Rectanglei( 175, 0, 175, 48 ), gdn::Rectanglei( 350, 0, 175, 48 ) );

	closeButton = gui::Button( gdn::Vector2f( 244, 0 ),
							   "X", "X", "X",
							   gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				               *buttonSmallImage, *font, 15,
				               gdn::Rectanglei( 0, 0, 24, 24 ), gdn::Rectanglei( 24, 0, 24, 24 ), gdn::Rectanglei( 48, 0, 24, 24 ) );
}

void DialogOptions::Terminate()
{
	App.GameConfig.SetSoundVolume( soundSlider.GetCurrentScale() );
	App.GameConfig.SetMusicVolume( musicSlider.GetCurrentScale() );

	App.GameConfig.SetParticlesEnabled( particlesCheckbox.IsChecked() );

	App.GameConfig.SaveToFile();
}

void DialogOptions::Step( gdn::Event& Event )
{
	if ( closeButton.Process( Event ) == GUI_BUTTON_RELEASE )
	{
		static_cast< SceneMainMenu& >( Scene ).CloseDialog();
	}

	soundSlider.Process( Event );
	musicSlider.Process( Event );
	particlesCheckbox.Process( Event );
	if ( controlsButton.Process( Event ) == GUI_BUTTON_RELEASE )
	{
		static_cast< SceneMainMenu& >( Scene ).OpenDialog< DialogControls >();
	}
}

void DialogOptions::Draw( gdn::RenderImage& renderImage )
{
	renderImage.Clear();

	renderImage.Draw( background );

	renderImage.Draw( soundText );
	renderImage.Draw( musicText );
	renderImage.Draw( particlesText );

	soundSlider.Draw( renderImage );
	musicSlider.Draw( renderImage );
	particlesCheckbox.Draw( renderImage );
	controlsButton.Draw( renderImage );

	closeButton.Draw( renderImage );

	renderImage.Display();
}

